package com.wb.mas.ui.defer;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.wb.mas.R;
import com.wb.mas.data.DemoRepository;
import com.wb.mas.entity.DeferInfoResponse;
import com.wb.mas.ui.base.viewmodel.ToolbarViewModel;
import com.wb.mas.ui.refund.RefundActivity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Copyright (C) 2018
 * DeferViewModel
 * <p>
 * Description
 * 展期viewModel
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019/5/2 0002, mwl, Create file
 */
public class DeferViewModel extends ToolbarViewModel {

    public String orderId;

    public ObservableField<DeferInfoResponse.DataBean> deferBean = new ObservableField<>();

    public DeferViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    private void init() {
        setTitleText(getString(R.string.defer_title));
        setRightIconVisible(View.GONE);
        getDeferData();
    }


    private void getDeferData() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        addSubscribe(model.getDeferInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DeferInfoResponse>() {
                    @Override
                    public void accept(DeferInfoResponse deferInfoResponse) throws Exception {
                        dismissDialog();
                        if (deferInfoResponse!=null && deferInfoResponse.getData()!=null){
                            deferBean.set(deferInfoResponse.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        ToastUtils.showShort(throwable.getMessage());
                    }
                }));
    }


    /**
     * 展期点击事件
     */
    public BindingCommand deferOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            Bundle bundle = new Bundle();
            bundle.putString("orderId", deferBean.get().getOrderId());
            bundle.putString("approvedAmt", deferBean.get().getTotalFee());
            bundle.putInt("type", RefundActivity.TYPE_REFUND);
            startActivity(RefundActivity.class, bundle);
        }
    });
}
